package com.fyzk.utils.jc;

import android.content.Context;
import android.util.Log;

import com.fyzk.activity.lottery.jc.football.JCInfo;
import com.fyzk.activity.lottery.jc.football.JCInfo.SPInfo;
import com.fyzk.utils.LogUtil;
import com.fyzk.utils.LogUtils;
import com.fyzk.utils.LotteryIDUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import static java.lang.Double.parseDouble;

public class JZPrizeComputeUtil {

    List<Integer> betcodes;
    List<Boolean> isDanList;
    List<JCInfo> listInfos;
    Context context;
    int lotteryId;

    public JZPrizeComputeUtil(Context context, List<JCInfo> listInfos, int lotteryId) {
        this.context = context;
        this.listInfos = listInfos;
        this.lotteryId = lotteryId;
    }

    /**
     * 初始化注数必要信息
     */
    public void initListInfo(List<JCInfo> listInfos) {
        this.listInfos = listInfos;
        betcodes = new ArrayList<>();
        isDanList = new ArrayList<>();
        for (int i = 0; i < listInfos.size(); i++) {
            JCInfo jcTeamInfo = listInfos.get(i);
            int checkNum = jcTeamInfo.getOnclikNum();
            if (checkNum > 0) {
                isDanList.add(jcTeamInfo.isDan());
            }
            betcodes.add(checkNum);
        }
    }

    private List<Double> getMaxOddsArraysValue() {
        List<Double> listSelsp = new ArrayList<>();
        for (JCInfo jcInfo : listInfos) {
            int teamNum = jcInfo.getOnclikNum();
            if (teamNum > 0) {
                listSelsp.add(getMaxpl(jcInfo));
            }
        }
        return listSelsp;
    }

    private double getMaxpl(JCInfo jcInfo) {
        double maxPl = 0.0d;
        switch (lotteryId) {
            case LotteryIDUtils.JZSPFRSPF_LOTTERY_ID_INT:
                maxPl = getSPFRQSPFMax(jcInfo);
                break;
            case LotteryIDUtils.JZSPF_LOTTERY_ID_INT:
            case LotteryIDUtils.JZBF_LOTTERY_ID_INT:
            case LotteryIDUtils.JZZJQ_LOTTERY_ID_INT:
            case LotteryIDUtils.JZBQC_LOTTERY_ID_INT:
            case LotteryIDUtils.JZRFSPF_LOTTERY_ID_INT:
                Iterator<String> iterators = jcInfo.btnHGPojo.keySet().iterator();
                while (iterators.hasNext()) {
                    String key = iterators.next();
                    SPInfo spInfo = jcInfo.btnHGPojo.get(key);
                    if (spInfo.isSel) {
                        double selpl = 0.00;
                        if(!spInfo.spValue.contains("-")){
                            selpl = Double.parseDouble(spInfo.spValue);
                        }
                        if (selpl > maxPl) {
                            maxPl = selpl;
                        }
                    }
                }
                break;
            case LotteryIDUtils.JZHH_LOTTERY_ID_INT:
                maxPl = getHGMax(jcInfo);
                break;
        }
        return maxPl;
    }

    private double getSPFRQSPFMax(JCInfo info) {
        if (info.getOnclikNum() == 0) {
            return 0d;
        }
        String[] keyArray = {info.VS_3, info.VS_1, info.VS_0, info.LETVS_3, info.LETVS_1, info
                .LETVS_0};
        ArrayList<String> selArrs = new ArrayList<>();
        for (String key : keyArray) {
            if (info.btnHGPojo.get(key).isSel) {
                selArrs.add(key);
            }
        }
        CombinationUtil combinationUtil = new CombinationUtil(selArrs);
        List<String[]> combinationList = combinationUtil.getCombineResult();
        List<String[]> mixList = new ArrayList<>();
        List<String[]> spfrqspfGroupByLanca = getSPFRQSPFGroupByLanca(info);
        for (String[] arr1 : combinationList) {
            for (String[] arr2 : spfrqspfGroupByLanca) {
                if (Arrays.equals(arr1, arr2)) {
                    mixList.add(arr1);
                }
            }
        }
        double[] pls = new double[mixList.size() + selArrs.size()];
        int x = 0;
        for (String key : selArrs) {
            if (info.btnHGPojo.get(key).spValue.contains("-"))
                info.btnHGPojo.get(key).spValue = "0";
            pls[x] = Double.parseDouble(info.btnHGPojo.get(key).spValue);
            x++;
        }
        for (String[] arr : mixList) {
            double item = 0.0d;
            for (String str : arr) {
                item += Double.parseDouble(info.btnHGPojo.get(str).spValue);
            }
            pls[x] = item;
            x++;
        }
        Arrays.sort(pls);
        return pls[pls.length - 1];
    }

    private double getHGMax(JCInfo info) {
        LogUtil.e("赛事标准对照计算--------------------------------------");
        List<String[]> mixList = new ArrayList<>();
        JZHGGroup JZHGGroup = new JZHGGroup(info);
        List<StringArrayObj> hg = JZHGGroup.getAll();
        LogUtil.e("交集模式收集<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");

        for (StringArrayObj stringArrayObj : hg) {
            mixList.add(stringArrayObj.tagArr);
        }
        double[] pls = new double[mixList.size()];
        int x = 0;
        for (String[] arr : mixList) {
            double item = 0.0d;
            for (String str : arr) {
                item += Double.parseDouble(info.btnHGPojo.get(str).spValue);
            }
            pls[x] = item;
            x++;
        }
        Arrays.sort(pls);
        if (pls.length == 0) {
            return 1;
        }
        return pls[pls.length - 1];
    }


    public ArrayList<ArrayList<String>> pailieArray(ArrayList<ArrayList<String>> arrss) {
        if (arrss.size() == 1) {
            return arrss;
        } else {
            ArrayList<String> arrss0 = arrss.get(0);
            ArrayList<String> arrss1 = arrss.get(1);
            ArrayList<ArrayList<String>> arrayList0 = new ArrayList<>();

            for (String s0 : arrss0) {
                for (String s1 : arrss1) {
                    ArrayList<String> arr_item = new ArrayList<>();
                    arr_item.add(s0);
                    arr_item.add(s1);
                    arrayList0.add(arr_item);
                }
            }

            if (arrss.size() == 2) {
                return arrayList0;
            } else if (arrss.size() == 3) {
                return jisuan(arrayList0, arrss.get(2));
            } else {
                int i = 2;
                ArrayList<ArrayList<String>> arr = new ArrayList<>();
                while (arrss.size() > (i + 1)) {
                    arr = jisuan(jisuan(arrayList0, arrss.get(i)), arrss.get(i + 1));
                    i++;
                }
                return arr;
            }
        }

    }


    private ArrayList<ArrayList<String>> jisuan(ArrayList<ArrayList<String>> s1s,
                                                ArrayList<String> s2s) {
        ArrayList<ArrayList<String>> ss1 = new ArrayList<>();
        for (ArrayList<String> s1arr : s1s) {
            for (String str2 : s2s) {
                ArrayList<String> arr = new ArrayList<>();
                for (String str1_i : s1arr) {
                    arr.add(str1_i);
                }
                arr.add(str2);
                ss1.add(arr);
            }
        }
        return ss1;
    }


    public static ArrayList<String[]> getSPFRQSPFGroupByLanca(JCInfo info) {
        String handipcap = info.getHandicap();
        int handicap = Integer.parseInt(handipcap.replace("+", ""));
        ArrayList<String[]> group_all = new ArrayList<>();
        if (handicap < 0) {
            if (handicap == -1) {
                group_all.add(new String[]{info.VS_3, info.LETVS_1});
                group_all.add(new String[]{info.VS_3, info.LETVS_3});
                group_all.add(new String[]{info.VS_1, info.LETVS_0});
                group_all.add(new String[]{info.VS_0, info.LETVS_0});
            } else {//<-1
                group_all.add(new String[]{info.VS_3, info.LETVS_1});
                group_all.add(new String[]{info.VS_3, info.LETVS_3});
                group_all.add(new String[]{info.VS_1, info.LETVS_0});
                group_all.add(new String[]{info.VS_0, info.LETVS_0});
                group_all.add(new String[]{info.VS_3, info.LETVS_0});
            }
        } else {
            if (handicap == 1) {
                group_all.add(new String[]{info.VS_3, info.LETVS_3});
                group_all.add(new String[]{info.VS_1, info.LETVS_3});
                group_all.add(new String[]{info.VS_0, info.LETVS_1});
                group_all.add(new String[]{info.VS_0, info.LETVS_0});
            } else {//>1
                group_all.add(new String[]{info.VS_3, info.LETVS_3});
                group_all.add(new String[]{info.VS_1, info.LETVS_3});
                group_all.add(new String[]{info.VS_0, info.LETVS_1});
                group_all.add(new String[]{info.VS_0, info.LETVS_3});
                group_all.add(new String[]{info.VS_0, info.LETVS_0});
            }
        }
        return group_all;
    }

    private List<Double> getMinOddsArraysValue() {
        List<Double> listSelsp = new ArrayList<>();
        for (JCInfo jcInfo : listInfos) {
            int teamNum = jcInfo.getOnclikNum();
            if (teamNum > 0) {
                ArrayList<Double> btnSps = new ArrayList<>();
                Iterator<String> iterators = jcInfo.btnHGPojo.keySet().iterator();
                while (iterators.hasNext()) {
                    String key = iterators.next();
                    SPInfo spInfo = jcInfo.btnHGPojo.get(key);
                    if (spInfo.isSel) {
                        if(spInfo.spValue.contains("-")){
                            btnSps.add(0.00);
                        }else{
                            btnSps.add(parseDouble(spInfo.spValue));
                        }
                    }
                }
                double[] sps = new double[btnSps.size()];
                int ix = 0;
                for (Double d : btnSps) {
                    sps[ix] = d;
                    ix++;
                }
                Arrays.sort(sps);
                listSelsp.add(sps[0]);
            }
        }
        return listSelsp;
    }

    /**
     * 单关计算注数
     *
     * @return
     */
    public int getDanZhushu() {
        return getDanAAmt(betcodes);
    }

    /**
     * 单关投注计算注数
     *
     * @param betcodes
     * @return
     */
    public static int getDanAAmt(List<Integer> betcodes) {
        int zhushu = 0;
        for (int i = 0; i < betcodes.size(); i++) {
            zhushu += betcodes.get(i);
        }
        return zhushu;
    }

    /**
     * 根据选中的串关名称计算注数
     *
     * @param radioText 过关
     * @return
     */
    public double getDoublePrize(String radioText, boolean isMax) {
        double prize = 0;
        /* 选择2-3场过关投注的，单注最高奖金限额20万米；
         选择4-5场过关投注的，单注最高奖金限额50万米；
         选择6场过关投注的，单注最高奖金限额100万米。
         */
        if (radioText == null) {//单关
            prize = getDanZhushu();
        } else if (radioText.equals("2串1")) {
            prize = getDuoPrize(2, isMax);
        } else if (radioText.equals("3串1")) {
            prize = getDuoPrize(3, isMax);
        } else if (radioText.equals("4串1")) {
            prize = getDuoPrize(4, isMax);
        } else if (radioText.equals("5串1")) {
            prize = getDuoPrize(5, isMax);
        } else if (radioText.equals("6串1")) {
            prize = getDuoPrize(6, isMax);
        } else if (radioText.equals("7串1")) {
            prize = getDuoPrize(7, isMax);
        } else if (radioText.equals("8串1")) {
            prize = getDuoPrize(8, isMax);
        }
        return prize;
    }

    /**
     * 根据选中的串关名称计算注数
     *
     * @param radioText 过关
     * @return
     */
    public double getDoublePrize2(String radioText, List<JCInfo> childInfos, boolean isMax) {
        double prize = 0;
        /* 选择2-3场过关投注的，单注最高奖金限额20万米；
         选择4-5场过关投注的，单注最高奖金限额50万米；
         选择6场过关投注的，单注最高奖金限额100万米。
         */
        if (radioText.equals("2串1")) {
            prize = getDuoPrize2(2, childInfos, isMax);
        } else if (radioText.equals("3串1")) {
            prize = getDuoPrize2(3, childInfos, isMax);
        } else if (radioText.equals("4串1")) {
            prize = getDuoPrize2(4, childInfos, isMax);
        } else if (radioText.equals("5串1")) {
            prize = getDuoPrize2(5, childInfos, isMax);
        } else if (radioText.equals("6串1")) {
            prize = getDuoPrize2(6, childInfos, isMax);
        } else if (radioText.equals("7串1")) {
            prize = getDuoPrize2(7, childInfos, isMax);
        } else if (radioText.equals("8串1")) {
            prize = getDuoPrize2(8, childInfos, isMax);
        }
        return prize;
    }

    public double getDuoPrize2(int select, List<JCInfo> childInfos, boolean isMax) {
        initListInfo(childInfos);
        int isDanNum = getIsDanNum(isDanList);
        // 多串M串N过关时选择胆，计算最大值时不考虑胆的数量
        if (isMax) {
            isDanNum = 0;
            return JCPrizePermutationandCombination.getFreedomGuoGuanMaxPrize(
                    getMaxOddsArraysValue(), select, isDanList, isDanNum);
        } else {
            return JCPrizePermutationandCombination.getFreedomGuoGuanMixPrize(
                    getMinOddsArraysValue(), select, isDanList, isDanNum);
        }
    }

    /**
     * 获取设胆个数
     *
     * @return
     */
    public int getIsDanNum(List<Boolean> isDanList) {
        int num = 0;
        for (int i = 0; i < isDanList.size(); i++) {
            if (isDanList.get(i)) {
                num++;
            }
        }
        return num;
    }

    public double getDuoPrize(int select, boolean isMax) {
        initListInfo(listInfos);
        int isDanNum = getIsDanNum(isDanList);
        if (isMax) {
            return JCPrizePermutationandCombination.getFreedomGuoGuanMaxPrize(
                    getMaxOddsArraysValue(), select, isDanList, isDanNum);
        } else {
            return JCPrizePermutationandCombination.getFreedomGuoGuanMixPrize(
                    getMinOddsArraysValue(), select, isDanList, isDanNum);
        }
    }

    public double getDanPrize(boolean isMax) {
        double prizeValue = 0;
        if (isMax) {
            List<Double> prizes = getMaxOddsArraysValue();
            for (Double sp : prizes) {
                prizeValue += sp;
            }
        } else {
            List<Double> prizes = getMinOddsArraysValue();
            double[] prizeSp = new double[prizes.size()];
            int index = 0;
            for (Double sp : prizes) {
                prizeSp[index] = sp;
                index++;
            }
            Arrays.sort(prizeSp);
            prizeValue = prizeSp[0];
        }
        return prizeValue * 2;
    }


    public String getDanGuanPrizeStr(int bei) {
        return JCPrizePermutationandCombination.getNewDanGuanPrize(getMaxOddsArraysValue(), bei);
    }
}
